package com.example.hewei.smarttransport.Dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

import com.example.hewei.smarttransport.AppClient;
import com.example.hewei.smarttransport.Request.BaseRequest;
import com.example.hewei.smarttransport.Request.RequestThread;

/**
 * Created by 何伟 on 2017/3/7.
 */

public class BaseDialog extends DialogFragment {

    public void setContext(Context mContext) {
        this.mContext = mContext;
    }

    protected Context mContext;
    protected AppClient mApp;

    public BaseDialog() {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.mApp = (AppClient) getActivity().getApplication();
        mApp.setActivity(getActivity());
    }

    /**
     * 显示对话框
     *
     * @param title   对话框标题
     * @param message 对话框内容
     */
    protected void showAlertDialog(String title, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(mContext.getString(android.R.string.ok), null);
        builder.show();
    }

    /**
     * 启动网络连接线程
     *
     * @param request 请求基类
     */
    protected void startRequest(BaseRequest request) {
        if (request != null) {
            RequestThread thread = new RequestThread(mContext,
                    mApp.getHandler());
            thread.setRequest(request);
            thread.start();
        }
    }
}
